package com.mb.app.appused.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

/**
 * 方块
 * 
 * @author wo2app
 * @email wo2app@qq.com
 * @time 2015年1月8日
 * @version Vx.x.x
 * @function veiwpage标题滚动条
 */
public class RectView extends View {

	private Paint paint;
	private Paint bgPaint;

	private float screenWidth;
	private int cont = 4;
	private float rectX, rectY, rectW, rectH;

	public RectView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public RectView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public RectView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		// TODO Auto-generated constructor stub
		init(context);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	/**
	 * 初始化内容
	 * 
	 * @param context
	 */
	@SuppressWarnings("deprecation")
	private void init(Context context) {
		WindowManager screen = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = screen.getDefaultDisplay();
		screenWidth = display.getWidth();
		rectX = 0;
		rectY = 0;
		rectW = screenWidth / cont;
		rectH = 100;

		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(0XFFFF0000);
		paint.setStyle(Paint.Style.FILL);

		bgPaint = new Paint();
		bgPaint.setAntiAlias(true);
		bgPaint.setColor(0xFFFFFFFF);
		bgPaint.setStyle(Paint.Style.FILL);
	}

	/**
	 * 设置页面数量
	 * 
	 * @param page
	 */
	public void setPageCont(int page) {
		this.cont = page;
		rectW = screenWidth / cont;
		invalidate();
	}

	/**
	 * 设置宽度
	 * 
	 * @param width
	 */
	public void setViewWidth(int width) {
		this.screenWidth = width;
	}

	/**
	 * 更新界面
	 */
	public void updateView() {
		this.invalidate();
	}

	/**
	 * 设置颜色
	 * 
	 * @param color
	 */
	public void setColor(int color) {
		paint.setColor(color);
		invalidate();
	}

	/**
	 * 设置背景颜色
	 */
	public void setBackgroundColor(int color) {
		bgPaint.setColor(color);
		invalidate();
	}

	/**
	 * 设置滑动
	 * 
	 * @param page
	 * @param position
	 */
	public void refRect(int page, int position) {
		rectX = (position / screenWidth) * (screenWidth / cont)
				+ (screenWidth / cont) * page;
		// rectW = rectX + screenWidth / cont;
		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		screenWidth = this.getWidth();
		rectW = rectX + screenWidth / cont;
		canvas.drawRect(0, 0, screenWidth, screenWidth, bgPaint); // 绘制背景色
		canvas.drawRect(rectX, rectY, rectW, rectH, paint); // 绘制矩形
	}

}
